unit USynthaxAnTests;

interface

uses
  TestFrameWork, synthax, Steps, ConceptualSchemes;

type
  TUSynthaxAnTests = class(TTestCase)
  private
    FConstituents: TConceptualScheme;
    ItWas: Boolean;
    function ConvertExpression(Expr: string): WideString;
    function BackConvert(Expr: WideString): string;
  protected
    procedure SetUp; override;
    procedure TearDown; override;
  published
    procedure testParse1;
    procedure testParse2;
    procedure testParse3;
  end;

implementation
uses LanguageExceptions;
{ TUSynthaxAnTests }

function TUSynthaxAnTests.BackConvert(Expr: WideString): string;
var
  i: Integer;
begin
  SetLength(Result, Length(Expr));
  for i := 1 to Length(Expr) do
    case Expr[i] of
      #$212C: Result[i] := 'B';
      #$00D7: Result[i] := '*';
    else
      Result[i] := Char(Expr[i]);
    end;
end;

function TUSynthaxAnTests.ConvertExpression(Expr: string): WideString;
var
  i: Integer;
begin
  SetLength(Result, Length(Expr));
  for i := 1 to Length(Expr) do
    case Expr[i] of
      'B': Result[i] := #$212C;
      '*': Result[i] := #$00D7;
    else
      Result[i] := WideChar(Expr[i]);
    end;
end;


procedure TUSynthaxAnTests.SetUp;
begin
  inherited;
  FConstituents := TConceptualScheme.Create(nil);
  FConstituents.Add('X1', '', '');
  FConstituents.Add('X2', '', '');
  FConstituents.Add('X3', '', '');
end;

procedure TUSynthaxAnTests.TearDown;
begin
  inherited;
  FConstituents.Free;
end;

procedure TUSynthaxAnTests.testParse1;
begin

  CheckEquals('X1', ParseExpr(ConvertExpression('D1X1'), FConstituents).Text);

  CheckEquals('X1*X2', BackConvert(ParseExpr(ConvertExpression('D1X1 * X2'), FConstituents).Text));

  CheckEquals('B(X1)', BackConvert(ParseExpr(ConvertExpression('D1B ( X1 )'), FConstituents).Text));

  CheckEquals('BBBB(X1)', BackConvert(ParseExpr(ConvertExpression('D1BBBB ( X1 )'), FConstituents).Text));

  ItWas := False;
  try
    ParseExpr('', FConstituents);
  except
    on E: ESynthaxError do ItWas := True;
  end;
  Check(ItWas);
end;

procedure TUSynthaxAnTests.testParse2;
begin
  ItWas := False;
  try
    ParseExpr('D1X1~', FConstituents);
  except
    on E: ESynthaxError do
      begin
        CheckEquals(5, E.Position);
        ItWas := True;
      end;
  end;
  Check(ItWas);

  ItWas := False;
  try
    ParseExpr(ConvertExpression('D1X1*'), FConstituents);
  except
    on E: ESynthaxError do
      ItWas := True;
  end;

  ItWas := False;
  try
    ParseExpr(ConvertExpression('D1(X1B'), FConstituents);
  except
    on E: ESynthaxError do
      begin
        CheckEquals(6, E.Position);
        ItWas := True;
      end;
  end;


  Check(ItWas);
end;

procedure TUSynthaxAnTests.testParse3;
begin

  CheckEquals('X1*X2*X3', BackConvert(ParseExpr(ConvertExpression('D1 X1 * X2 * X3'), FConstituents).Text));

  CheckEquals('X1*(X1*X2*X3)', BackConvert(ParseExpr(ConvertExpression('D1 X1 * (X1 * X2*X3)'), FConstituents).Text));

  CheckEquals('X1*(B(X1)*X2)*(X3*B(X2))', BackConvert(ParseExpr(ConvertExpression('D1 X1 * (B(X1) * X2) *(X3*B(X2))'), FConstituents).Text));

  CheckEquals('(X1*X2*X3)*B(X2)', BackConvert(ParseExpr(ConvertExpression('D1 (X1* X2 * X3)*B(X2)'), FConstituents).Text));

  CheckEquals('BBB(X1*X2*X3)*BB(X2)', BackConvert(ParseExpr(ConvertExpression('D1 BBB(X1* X2 * X3)*BB(X2)'), FConstituents).Text));
end;


initialization

  TestFramework.RegisterTest('USynthaxAnTests Suite',
    TUSynthaxAnTests.Suite);

end.

 